import {
	mapState,
	mapMutations
} from "vuex"
import {
	userLogin,
	getMarketInfo,
	getCartNumber
} from "./api.js"
export default {
	data() {
		return {
			categories: [{
					name: '全部分类',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '特殊熟食',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '休闲小吃',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '一元系列',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '饮料系列',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '夏日系列',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '早餐系列',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				},
				{
					name: '糖果系列',
					icon: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.aMNqrD9Xi0OxD_50t-qyqwHaHa?w=194&h=194&c=7&r=0&o=5&pid=1.7'
				}
			]
		}
	},
	onShow() {

	},
	onLoad(options) {
		this.getData();
	},
	computed: {

	},
	methods: {
		//   vuex储存信息
		...mapMutations([
			"setCartCount",
			"setShopInfo", //设置店铺信息
			"setShopPhone", //设置电话
			"setShopStatus", //设置店铺状态
			"initdishListMut", //设置购物车订单
			"setStoreInfo",
			"setBaseUserInfo", //设置用户基本信息
			"setLodding",
			"setToken", //设置token
			"setDeliveryFee", //设置配送费
		]),
		// 从vuex信息
		...mapState([
			"cartCount",
			"shopInfo", //店铺信息
			"shopPhone", //电话
			"orderListData",
			"baseUserInfo", //用户信息
			"lodding",
			"token", //token
			"deliveryFee", //配送费
		]),
		// 获取首页店铺信息
		async getShopInfo() {
			await getShopStatus()
				.then((res) => {
					this.shopStatus = res.data
					// console.log(res.data);
					this.setShopStatus(res.data)
				})
				.catch((err) => {})
		},
		// 获取用户信息
		getData() {
			// console.log('cartCount', this.cartCount());
			// this.setCartCount(4)
			// console.log('cartCount', this.cartCount());

			let res = wx.getMenuButtonBoundingClientRect()
			// let _this = this
			// 获取店铺状态
			// this.getShopInfo()
			// this.selectHeight = res.height
			let jsCode = ""
			uni.login({
				provider: "weixin",
				success: (loginRes) => {
					//console.log('login的loginRes', loginRes);
					if (loginRes.errMsg === "login:ok") {
						jsCode = loginRes.code
					} else {
						uni.showToast({
							title: '系统错误，联系管理员',
							icon: "fail",
							duration: 1000,
						});
					}
				},
			})
			const params = {
				code: jsCode,
				// 传递地理位置信息
			}
			let _this = this
			userLogin(params)
				.then((success) => {
					_this.setToken(success.data.token)
					getCartNumber().then((res) => {
						this.setCartCount(res.data)
					}).catch((error) => {})
				}).catch((error) => {
					console.error('userLogin Error:', error);
				})
		},
		getCartN() {
			// 获取购物车的数量
			getCartNumber().then((res) => {
				console.log('res', res);
				console.log('cartCount', this.cartCount());
				if (res.code == 200) {
					this.setCartCount(res.data)
				}
			}).catch((error) => {})
		}
	},

}